//
//  HJTabBarItem.swift
//  WeiBo
//
//  Created by 韩俊 on 15/12/16.
//  Copyright © 2015年 HanJun. All rights reserved.
//

import UIKit

class HJTabBarItem: UITabBarItem {
    
    override var badgeValue: String? {
        
        didSet{
            
            printLog(111)
            if badgeValue == nil {
                return
            }
            
            let controller = valueForKeyPath("_target") as! UITabBarController
            for tabBarChild in controller.tabBar.subviews {
                
                
                if tabBarChild.isKindOfClass(NSClassFromString("UITabBarButton")!){
                    
                    for tabBarButtonChild in tabBarChild.subviews {
                        
                        
                        if tabBarButtonChild.isKindOfClass(NSClassFromString("_UIBadgeView")!){
                            
                            for badgeViewChild in tabBarButtonChild.subviews{
                                
                                if badgeViewChild.isKindOfClass(NSClassFromString("_UIBadgeBackground")!){
                                    
                                    
                                    var count:UInt32 = 0
                                    //通过运行时 获取其身上的属性或成员变量
                                    let ivars = class_copyIvarList(NSClassFromString("_UIBadgeBackground")!, &count)
                                    
                                    for i in 0..<count {
                                        let ivar = ivars[Int(i)]
                                        let name = ivar_getName(ivar)
                                        let nameString = NSString(CString: name, encoding: NSUTF8StringEncoding)
                                        
                                        if (nameString!.isEqualToString("_image")){
                                            badgeViewChild.setValue(UIImage(named: "main_badge"), forKey: nameString as! String)
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            
//            var hjcount:UInt32 = 0
//            //通过运行时 获取其身上的属性或成员变量
//            let hjivars = class_copyIvarList(NSClassFromString("UITabBarItem")!, &hjcount)
//            
//            for i in 0..<hjcount {
//                
//                let ivar = hjivars[Int(i)]
//                let name = ivar_getName(ivar)
//                let nameString = NSString(CString: name, encoding: NSUTF8StringEncoding)
//                
//                printLog(nameString)
//                
//            }
    }
    
    
}

}
